#!/usr/local/bin/python
# $Id$
# Author: Fred Chu
# Date: 2007-12
# License: MIT
import web
import config
from sqlobject import *
import datetime
from model import Record

render = web.template.render('templates/',cache=False)
urls = (
        '/(print)?', 'index',
        '/post/?', 'post',
        '/active/(\d+)', 'active'
)

# Index page
class index:
   def GET(self, name):
       today = str(datetime.date.today())
       records=Record.select(OR(Record.q.active != "0", LIKE(Record.q.modified, today + "%")), 
                        orderBy='created ASC')
       if not name:
           print render.index(records,config)
       elif name == 'print':
           print render.print_type(records,today,config)

# Post page
class post:
    def GET(self):
        print render.post(config)
          
    def POST(self):
        i = web.input()
        if i.content == '':
            print render.post(config)
        else:    
            new = Record(content=i.content)
            web.seeother(config.site_url)    

# active yes/no work record            
class active:            
    def GET(self, name):
        record = Record.select(Record.q.id==name)
        if record[0].active == 1:
            record[0].active = 0
        else:
            record[0].active = 1
        record[0].modified = datetime.datetime.now()
        web.seeother(config.site_url)

# run as Apache fastcgi       
def runfcgi_apache(func):
    web.wsgi.runfcgi(func, None)    
    
# Not Found Page Here
def page_notfound():
    print render.page_notfound()
    
web.webapi.notfound = page_notfound        
web.webapi.internalerror = web.debugerror    

# Run Web app    
if __name__ == "__main__":
   web.wsgi.runwsgi = runfcgi_apache
   web.run(urls, globals(), web.reloader)
